<?php

class CurrentUser
{
    public $_AccessUrl
            , $_ActiveCode
            , $_Address
            , $_Birthday
            , $_ChangePassCode
            , $_City
            , $_Country
            , $_CreatedDate
            , $_Email
            , $_Fax
            , $_FullName
            , $_IdentifyID
            , $_IsActive
            , $_PassCodeExpire
            , $_Password
            , $_Phone
            , $_ServiceTransactions
            , $_Sex
            , $_UserGroup
            , $_UserID
            , $_UserLogs
            , $_UserUsingServices
            , $_Username
            , $_isLoggedIn = false;

    /**
     *
     * @var CurrentUser
     */
    protected static $_instance = null;

    public function getAccessUrl()
    {
        return $this->_AccessUrl;
    }

    public function getActiveCode()
    {
        return $this->_ActiveCode;
    }

    public function getAddress()
    {
        return $this->_Address;
    }

    public function getBirthday()
    {
        return $this->_Birthday;
    }

    public function getChangePassCode()
    {
        return $this->_ChangePassCode;
    }

    public function getCity()
    {
        return $this->_AccessUrl;
    }

    public function getCountry()
    {
        return $this->_Country;
    }

    public function getCreatedDate()
    {
        return $this->_CreatedDate;
    }

    public function getEmail()
    {
        return $this->_Email;
    }

    public function getFax()
    {
        return $this->_Fax;
    }

    public function getFullName()
    {
        return $this->_FullName;
    }

    public function getIdentifyID()
    {
        return $this->_IdentifyID;
    }

    public function getIsActive()
    {
        return $this->_IsActive;
    }

    public function getPassword()
    {
        return $this->_Password;
    }

    public function getPassCodeExpire()
    {
        return $this->_PassCodeExpire;
    }

    public function getPhone()
    {
        return $this->_Phone;
    }

    public function getServiceTransactions()
    {
        return $this->_ServiceTransactions;
    }

    public function getSex()
    {
        return $this->_Sex;
    }

    public function getUserGroup()
    {
        return $this->_UserGroup;
    }

    public function getUserID()
    {
        return $this->_UserID;
    }

    public function getUserLogs()
    {
        return $this->_UserLogs;
    }

    public function getUserUsingServices()
    {
        return $this->_UserUsingServices;
    }

    public function getUsername()
    {
        return $this->_Username;
    }

    public function isLoggedIn()
    {
        return $this->_isLoggedIn;
    }

    /**
     *
     * @return CurrentUser
     */
    static public function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
            self::$_instance->checkCookie();
        }

        return self::$_instance;
    }

    /**
     *
     * @return CurrentUser
     */
    public function checkCookie()
    {
        // check user from SESSION first
        if (isset($_SESSION['loggedInUser']) && $_SESSION['loggedInUser']->_UserID > 0) {
            foreach ($_SESSION['loggedInUser'] as $property => $value)
            {
                $this->$property = $value;
            }
            $this->_isLoggedIn = true;
            return $this;
        }

        $cookieUser = isset($_COOKIE['usr']) ? $_COOKIE['usr'] : null;
        $cookiePassword = isset($_COOKIE['pwd']) ? $_COOKIE['pwd'] : null;

        if (null !== $cookieUser && null !== $cookiePassword) {
            $user = Http::SOAPClient()->getUserByUserNameAndPassword(array('userName' => $cookieUser, 'password' => $cookiePassword, 'ipAddress' => ''));

            if ($user->getUserByUserNameAndPasswordResult && $user->getUserByUserNameAndPasswordResult->UserID > 0) {
                foreach ($user->getUserByUserNameAndPasswordResult as $property => $value)
                {
                    $variable = '_' . $property;
                    $this->$variable = $value;
                }
                $this->_Password = $cookiePassword;
                $this->_isLoggedIn = true;
                $_SESSION['loggedInUser'] = $this;
            }
        }

        return $this;
    }

    protected function __construct() {}
    private function __clone() {}
}